﻿using System;
using System.Linq;
using System.Text;
using System.Web.UI;
using Umb.SimpleWebsite.Shared.Content.BackOffice;
using umbraco.cms.businesslogic.datatype;
using umbraco.interfaces;
using umbraco.cms.businesslogic;

namespace Umb.SimpleWebsite.Shared.DataTypes.SingleItemPicker
{
    public class PropertyDataEditor: BaseFTypeEditor, IDataEditor
    {
        public string ControlName{get;set;} 
        public PropertyDataEditor(IData internalData, AbstractDataEditor baseFType):base (internalData, baseFType)
        {            
            ControlName=baseFType.DataTypeName.Replace(" ",""); 
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            BasePropertyType.AddClientDependsObj(ControlName, this);
        }

        protected override void CreateChildControls()
        {
            base.CreateChildControls();//needs to generate basic functionality at least textcontrol
            string resFieldId = txtInnerData.ClientID;
            GenerateHtmlInterface(resFieldId, this, DataToShow);
        }

        private void GenerateHtmlInterface(string resFieldId,PropertyDataEditor propertyDataEditor,string DataToShow)
        {
            
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<script type=\"text/javascript\" language=\"javascript\">");
            sb.AppendLine("  jQuery(document).ready(function(){");
            sb.AppendLine("     var cdp=new ISM_SimpleItemPicker('" + resFieldId + "',{prevalue:" + GetPreValue() + ",content:{},media:{}});");
            sb.AppendLine("     cdp.show();");
            sb.AppendLine("  })");
            sb.AppendLine("</script>");

            LiteralControl lc = new LiteralControl(); lc.ID = this.ID + "_ld";
            lc.Text = sb.ToString();
            this.Controls.Add(lc);

        }
        private string GetPreValue()
        {
             PropertyPreValues.PreValueValue pvs=((PropertyPreValues)PreValueEditor).ValueObject;
            if (pvs==null) return "{}";
             PropertyPreValues.PreValueValue.Picker prevalue=pvs.pickers.Where(pi => pi.name.Equals(pvs.current, StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
            if (prevalue==null) return "{}";
             //get prevalue for selected picker type
            if (prevalue.name.Equals("content") && prevalue.value != null)
            {
                if (prevalue.value.name.Equals("exact")) prevalue.start = prevalue.value.text;
                else if (prevalue.value.name.Equals("currt")) prevalue.start = CurPageId;
                else if (prevalue.value.name.Equals("levln"))//node by levle
                {
                    int level = 0;
                    if (int.TryParse(prevalue.value.text, out level)) prevalue.start = BackOfficeContext.ParentNodeByLevel(level).Id.ToString();
                    else prevalue.start = CurPageId;
                }
                else if (prevalue.value.name.Equals("parnt"))//parent node by level
                {
                    int level = 0;
                    if (int.TryParse(prevalue.value.text, out level))
                    {
                        umbraco.cms.businesslogic.CMSNode cn = BackOfficeContext.CurNode;
                        level = cn.Level - level;
                        prevalue.start = BackOfficeContext.ParentNodeByLevel(level).Id.ToString();
                    }
                    else prevalue.start = CurPageId;
                }
                else if (prevalue.value.name.Equals("stype"))//node by type from top
                {
                    CMSNode n = BackOfficeContext.GetChildOfType(prevalue.value.text, BackOfficeContext.ParentNodeByLevel(1).Id);
                    if (n != null) prevalue.start = n.Id.ToString();
                }
                else if (prevalue.value.name.Equals("tprop"))//node by property value
                {
                    umbraco.cms.businesslogic.Content c = new umbraco.cms.businesslogic.Content(BackOfficeContext.ParentNodeByLevel(1).Id);
                    umbraco.cms.businesslogic.property.Property p = c.getProperty(prevalue.value.text);
                    if (p != null) prevalue.start = System.Convert.ToString(p.Value);
                }

            }
            else if (prevalue.value != null)
            {
                prevalue.start = prevalue.value.text;//for not content nodes it just text value
            }
            System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
            string result = ser.Serialize(prevalue);
            return result;             
        }
        
        
        public string CurPageId
        {
            get { 
                umbraco.cms.businesslogic.CMSNode cn=BackOfficeContext.CurNode;
                return cn != null ? cn.Id.ToString() : "-1";  
            }
        }


       
        
    }
}
